using System;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.Common;
using System.Data;

namespace Tort.Utility
{
    /// <summary>
    /// Class to allow native type coversion into specific parameter types
    /// rather than relying on DbTypes completely
    /// </summary>
    internal static class ParameterBuilder
    {
        internal static Object GetParameter(string columnName, string parameterName, object columnValue, Type nativeType, Database.Engine.DBEngine engine)
        {
            return GetParameter(columnName, parameterName, columnValue, nativeType, engine, false);
        }

        internal static Object GetParameter(string columnName, string parameterName, object columnValue, Type nativeType, Database.Engine.DBEngine engine, bool isPrimeKeyRequired)
        {
            DbParameter param = null;
            switch (engine)
            {
                case Database.Engine.DBEngine.SqlServer2000:
                case Database.Engine.DBEngine.SqlServer2005:
                    if (!isPrimeKeyRequired)
                    {
                        param = GetSQLParameter(parameterName, columnValue, nativeType);
                    }
                    else
                    {
                        param = GetIdentity(columnName, parameterName, nativeType);
                    }
                    break;
                case Database.Engine.DBEngine.Oracle9i:
                case Database.Engine.DBEngine.Oracle10g:
                    param = GetOracleParameter(parameterName, columnValue, nativeType);
                    break;
            }
            return param;
        }

        private static SqlParameter GetSQLParameter(string parameterName, object columnValue, Type nativeType)
        {
            SqlParameter param = new SqlParameter();
            param.ParameterName = parameterName;
            param.SqlDbType = new Conversion.DbTypeConverter().ConvertToSqlDbType(nativeType);
            param.Value = columnValue == null ? DBNull.Value : columnValue;
            return param;
        }

        private static OracleParameter GetOracleParameter(string parameterName, object columnValue, Type nativeType)
        {
            OracleParameter param = new OracleParameter();
            param.ParameterName = parameterName;
            param.OracleType = new Conversion.DbTypeConverter().ConvertToOracleDbType(nativeType);
            param.Value = columnValue == null ? DBNull.Value : columnValue;
            return param;
        }

        internal static SqlParameter GetIdentity(string columnName, string parameterName, Type nativeType)
        {
            SqlParameter param = new SqlParameter();
            param.Direction = ParameterDirection.Output;
            param.ParameterName = parameterName;
            param.SqlDbType = new Conversion.DbTypeConverter().ConvertToSqlDbType(nativeType);
            param.SourceColumn = columnName;
            return param;
        }
    }
}